import random

from osc05.utils import global_var


class Transportation:
    def __init__(self, env, construction_site):
        self.env = env
        self.construction_site = construction_site

    # ERROR ERROR ERROR ERROR
    # fill the storage yard with components
    def transport(self):
        for activity in self.construction_site.activities:
            # if actual delivery is earlier than schedule, take is as 0.
            delay = max(0, self.get_delivery_delay())
            activity.act_delivery_time = activity.skd_delivery_time + delay
            yield self.env.timeout(activity.act_delivery_time)
            activity.component_delivered_event.succeed()
            print('***@', self.env.now, activity.name, activity.act_delivery_time)
        # assembling_ex_wall_num = 24
        # for i in range(assembling_ex_wall_num):
        #     ex_wall_component = Component(name=('EW' + str(i + 101).zfill(len(str(assembling_ex_wall_num)))), vol=4.32,
        #                                   weight=2.12)
        #     yield self.construction_site.storage_yard.put(ex_wall_component, 'STOCK IN EW', ex_wall_component)
        #
        # assembling_in_wall_num = 24
        # for i in range(assembling_in_wall_num):
        #     in_wall_component = Component(name=('IW' + str(i + 101).zfill(len(str(assembling_ex_wall_num)))), vol=3.62,
        #                                   weight=1.82)
        #     yield self.construction_site.storage_yard.put(in_wall_component, 'STOCK IN IW', in_wall_component)

    def get_delivery_delay(self):
        if global_var.get_value('STOCHASTIC_MODE'):
            component_delivery_delay = random.gauss(10, 20)
        else:
            component_delivery_delay = 0
        return component_delivery_delay
